package com.sicheng.lc.周赛.分类.哈西表or计数or滑动窗口or双指针.滑动窗口;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/6/14 9:55
 */
public class 替换字符串匹配 {
    //    https://leetcode.cn/problems/match-substring-after-replacement/
    public static void main(String[] args) {

        替换字符串匹配 solution = new 替换字符串匹配();

        String s = "fool3e7bar", sub = "leet";
        char[][] mappings = {
                {'e', '3'},
                {'t', '7'},
                {'t', '8'}
        };
        System.out.println(solution.matchReplacement(s, sub, mappings));


    }


    Map<Character, Set<Character>> map = new HashMap<>();

    public boolean matchReplacement(String s, String sub, char[][] mappings) {
        if (sub.length() > s.length())
            return false;

        for (char[] mapping : mappings) {
            Set<Character> set = map.computeIfAbsent(mapping[0], k -> new HashSet<>());
            set.add(mapping[1]);
        }

        for (int i = 0; i < s.length() - sub.length() + 1; i++) {
            if (match(i, s, sub))
                return true;

        }

        return false;
    }

    private boolean match(int start, String s, String sub) {
        for (int i = start; i < start + sub.length(); i++) {
            if (s.charAt(i) != sub.charAt(i - start)
                    && (!map.containsKey(sub.charAt(i - start)) || !map.get(sub.charAt(i - start)).contains(s.charAt(i)))) {
                return false;
            }
        }
        return true;
    }
}
